import { Controller, Get, Query, Headers } from "@midwayjs/core";
import { Body, Inject, Post } from "@midwayjs/decorator";
import { UserService } from "../service/user.service";
import { SexType } from "../types/user.type";


@Controller('/v1/user')
export class UserController {

  @Inject()
  userService: UserService;


  @Get('/')
  async getUser(@Query('uid') uid: string) {
    return await this.userService.getUserByUid(uid);
  }

  @Post('/token')
  async getUserByToken(@Headers() headers): Promise<any> {
    return this.userService.getUserByToken(headers.token)
  }

  @Post('/login')
  async login(@Body() user: { email: string, password: string }): Promise<any> {
    return this.userService.login(user.email, user.password)
  }

  @Post('/get')
  async get(@Body() user: { uid: string }): Promise<any> {
    return this.userService.getUserByUid(user.uid)
  }

  @Post('/list')
  async list(@Body() info: { page: number, number: number }): Promise<any> {
    const { page, number } = info;
    return this.userService.list(page, number);
  }

  @Post('/register')
  async register(@Body() user: { password: string, secret: string, code: string }): Promise<any> {
    const { password, secret, code } = user;
    return await this.userService.register(password, secret, code)
  }

  @Post('/forgotpassword')
  async forgotpassword(@Body() user: { password: string, secret: string, code: string }): Promise<any> {
    const { password, secret, code } = user;
    return await this.userService.forgotpassword(password, secret, code)
  }

  @Post('/updated')
  async updated(@Body() user: { uid: string, avatar?: string, name?: string, nick: string, birthday: number, motto: string, sex: SexType }): Promise<any> {
    const { uid, avatar, name, nick, birthday, motto, sex } = user;
    return await this.userService.updated(uid, avatar, name, nick, birthday, motto, sex)
  }

  @Post('/password')
  async updatedPassword(@Body() user: { uid: string, oldpsw: string, newpsw: string }): Promise<any> {
    const { uid, oldpsw, newpsw } = user;
    return await this.userService.updatedPassword(uid, oldpsw, newpsw)
  }
}